<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <script type="text/javascript" src="<%=basePath %>admin/test/upload/jquery-latest.pack.js"></script>
	<link rel="stylesheet" type="text/css" href="<%=basePath %>admin/test/css/easyui.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>admin/test/css/icon.css">
	<script type="text/javascript" src="<%=basePath %>admin/test/js/jquery-1.3.2.min.js"></script>
	<script type="text/javascript" src="<%=basePath %>admin/js/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="<%=basePath %>admin/js/easyui-lang-zh_CN.js"></script>
	
	<!-- ajax异步加载二级联动菜单 -->
<!-- ajax异步加载二级联动菜单 -->
 <script type="text/javascript">
  //创建连接，判断浏览器 
    var http_request=false; 
    function createXMLHttpRequest()
    { 
	    if(window.XMLHttpRequest)
	    { 
	      XMLHttpReq=new XMLHttpRequest(); //创建对象对应不同的浏览器
	    } 
	    else if(window.ActiveXObject)
	    { 
		      try
		      { 
		         XMLHttpReq=new ActiveXObject("Msxml2.XMLHTTP"); 
		      }
		      catch(e)
		      { 
		
			      try
			      { 
			         XMLHttpReq=new ActiveXObject("Microsoft.XMLHTTP"); 
			      }
			      catch(e)
			      { 
			
			      } 
	
	        } 
	    } 

    } 

//onchange事件 
    function changeHospital(){//当第一个下拉框的选项发生改变时调用该函数
    //var province=document.getElementById("provice").value; //获取第一级菜单的值
    var hospital=$('#hospital').val();
 
    sendRequest("<%=basePath%>queryKeshiByHospitalzhuanjiaAction?hospital="+hospital); //发送参数到SERVLET
//值得注意的是URL的servlet地址前面不加“/”并且为小写字母（试验的结论）
    } 
    //发送请求 
    function sendRequest(url){ //发送函数
    createXMLHttpRequest(); 
    XMLHttpReq.open("GET",url,true); 
    XMLHttpReq.onreadystatechange=progessRequest;//调用相关的XML接受函数 
    XMLHttpReq.send(null); 
    } 

//判断接收请求 
    function progessRequest(){ 
    if(XMLHttpReq.readyState==4){ 
    if(XMLHttpReq.status==200){ 
         var xmlDoc=XMLHttpReq.responseXML; //获取返回的XML文档
         var citys=xmlDoc.getElementsByTagName("keshi");//获取XML节点
         var obj=document.getElementById("keshi");//获取第二级菜单的对象
    if(obj!=null)
    {//如果不为空就置空 
           obj.options.length=0; 
         } 

      for(var i=0;i<citys.length;i++){//用循环给select中加值 

         var xValue = citys[i].childNodes[0].firstChild.nodeValue;

         var xText = citys[i].childNodes[1].firstChild.nodeValue;

         var option = new Option(xText, xValue);

      try{

         obj.add(option);

      }catch(e){}

      } 

    }else{ 

    alert( "请求异常"+XMLHttpReq.responseText); 

    } 

    } 
   } 
  </script>
<script>
function editing()
     {
    	var selected = $('#test').datagrid('getSelected');
    	var str=new Array();
		if (selected)
		{
			//给页面赋值
			$('#id').val(selected.id);
			 $.ajax({
								   type: 'post',
								   url: "queryByIdzhuanjiaAction",
								   data: "info="+selected.id,
								   success: function(data)
								   {
								  
			var str=data.split("\t");
			$('#name').val(str[0]);
			$('#sex').val(str[1]);
			$('#hos').val(str[2]);
			$('#ks').val(str[3]);
			
			var kess=new Array();
		    kess=str[3].split(",");
		    for(var i=0;i<kess.length;i++)
		    {
		  
			document.all.ks.options[document.all.ks.options.length]=new Option(kess[i],kess[i]);
			}
			document.all( "ks").selectedIndex=1;
			
			$('#birthday').val(str[4]);
			$('#worktime').val(str[5]);
			$('#xueli').val(str[6]);
			$('#lczc').val(str[7]);
		
			$('#telphone').val(str[8]);
			$('#introduce').val(str[9]);
			$('#zc').val(selected.zc);
			$('#dd').show();
			$('#dd').dialog({
			
			});
									
									
								  }
							});
			
			
					
		}
    }
    

</script>

	<script>
		$(function(){
			$('#test').datagrid({
				title:'增加医生',
				 iconCls:'icon-ok',
				width:1060,
				height:700,
				pageNumber:1,
				pageSize:20,
				 pageList:[5,10,15,20,30,50,80,100],
                collapsible:true,
				nowrap: true,
				//fit:true,自动适应屏幕宽度
				striped: true,
				url:'<%=basePath%>initZhuanjiazhuanjiaAction',
				sortName: 'id',
				sortOrder:'desc',
				rownumbers:true,
				idField:'id',
				queryParam:{},
				singleSelect:false,
				frozenColumns:[[
                {field:'ck',checkbox:true}
              ]],
	        columns:[[           
            {field:'id',title:'编号',width:100,sortable:true,editor:'text'},
			{field:'name',title:'医生姓名',width:200,sortable:true,editor:'text'},
			{field:'sex',title: '医生性别',width:100,sortable:false,editor:'text'},
			{field:'ksid',title:'所属科室',width:200,sortable:false,editor:'text'},
			{field:'lczc',title:'医生职务',width:260,editor:'text'},
			{field:'operator',title:'操作列',width:150,align:'center',
          formatter:function(value,row,index){
        if(row.editing)
        {
            var s ='<a href="javascript:void(0);" onclick="saveRow('+index+')" style="text-decoration: none;color: #800080;">保存</a>  ';
            var c ='<a href="javascript:void(0);" onclick="cancelRow('+index+')" style="text-decoration: none;color: #800080;">取消</a>';
            return s+c;
        }
        else
        {
          var e = '<img src="<%=basePath%>admin/test/css/icons/btn_edit.png" onclick="editRow('+index+')" > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
          var d = '<img src="<%=basePath%>admin/test/css/icons/btn_delete.png" onclick="delRow('+index+')" > ';
          return e+d;
        }
    }
}
				]],
				pagination:true,
				rownumbers:true,
				toolbar:[{
             text:'增加',
             iconCls:'icon-add',
             handler:function()
             {
                  $('#add').show();
			//
			$('#add').dialog({
			//buttons:[{
					//	text:'增加',
						//iconCls:'icon-save',
						//handler:function()
						//{
							//addZhuanjia();
						//}
					//},{
						//text:'取消',
						//handler:function()
						//{
							//$('#add').dialog('close');
							//$("#test").datagrid("loaded");
						//}
					//}]
			});
             }
          },'-',
          {
  			  text:'查询',
              iconCls:'icon-search',
              handler:function()
              {
                  $('#query').window('open');
              }
          },'-',
          {
                    id:'btncut',
					text:'批量删除',
					iconCls:'icon-cut',
					handler:function()
					{
						delUserSelect();
					}
          }
        ],
		onSortColumn:function(sort,order)
		{
			alert(sort+":"+order)
		}
				
			});
		});
		/**/
		function add()
		{
		   
		    var sex=$('#sex1').val();
		    var name=$('#name1').val();
		    var birthday=$('#birthday1').val();
		    var worktime=$('#worktime1').val();
		    var xueli=$('#xueli1').val();
		    var jxzc=$('#lczc1').val();
		    var telphone=$('#telphone1').val();
		    var introduce=$('#introduce1').val();
		    var zc=$('#zc1').val();
		    var hos=$('#hid').val();
			var ks=$("#keshi").find("option:selected").text();
			$.ajax({
				   type: 'post',
				   url: "addZhuanjiazhuanjiaAction",
				   data: "info="+name+"\t"+sex+"\t"+jxzc+"\t"+birthday+"\t"+worktime+"\t"+xueli+"\t"+telphone+"\t"+introduce+"\t"+zc+"\t"+hos+"\t"+ks,
				   success: function(data)
				   {
				      alert(data);
			    		if(data=='ok')
			    		{
			    			$.messager.alert('后台提示', '数据添加成功', 'info');
			    			//初始化
			    			$('#add').dialog('close');
			    			$('#test').datagrid('reload');
			    		}
						
						if(data=='error')
						{
			    			$.messager.alert('后台提示', '数据添加失败', 'info');
			    			$('#add').dialog('close');
			    		}
					}
					
				  });
		}
		/*删除提示*/
		function delUserSelect(){
			var rows = $('#test').datagrid('getSelections');
			if(rows.length>0)
			{
				del();
			}
			else
			{
			   $.messager.alert('提醒','请选择一行','info');
				return false;
			}
		}
		//确认删除
		function  del()
		{
			$.messager.confirm('确认','你确定要删除吗?',function(r)
			{
			   if (r)
			   {
			    	myAjaxDel();
			   }	
	   		});
		}
		//删除操作
		function myAjaxDel()
		{
			var ids = [];
			var rows = $('#test').datagrid('getSelections');
			for(var i=0;i<rows.length;i++)
			{
				ids.push(rows[i].id);
			}
			$.ajax({
				   type: 'post',
				   url: "delZhuanjiazhuanjiaAction",
				   data: "delZhuanIds="+ids,
				   success: function(data)
				   {
				     if(data=='ok')
				     {
						$.messager.alert('提醒','删除成功','info');
						var rows2 = $('#test').datagrid('getSelections');//获取你选择的所有行
						//循环所选的行
						for(var i =0;i<rows2.length;i++)
						{
							var index2 = $('#test').datagrid('getRowIndex',rows2[i]);//获取某行的行号
							$('#test').datagrid('deleteRow',index2);	//通过行号移除该行
						}
					}
					if(data=='error')
					{
						$.messager.alert('提醒','删除失败','info');
					}
				  }
			});
      }
		
		/*获取选中的编号*/
		function getSelected()
		{
			var selected = $('#test').datagrid('getSelected');
			alert(selected.id+":"+selected.name);
			
		}
		/*获取所有选中的编号*/
		function getSelections()
		{
			var ids = [];
			var rows = $('#test').datagrid('getSelections');
			for(var i=0;i<rows.length;i++)
			{
				ids.push(rows[i].id);
			}
			
		}
		function myformatter(value,rec)
		{
			return 'a:'+value+'>'+rec.name;
		}
      
    //提交保存
	function updateZhuanjia()
	{
          var id=$('#id').val();
         
		    var sex=$('#sex').val();
		    var name=$('#name').val();
		   var lczc=$('#lczc').val();
		  
		    var birthday=$('#birthday').val();
		    var worktime=$('#worktime').val();
		    var xueli=$('#xueli').val();
		    var jxzc=$('#jxzc').val();
		    var telphone=$('#telphone').val();
		    var introduce=$('#introduce').val();
		    var zc=$('#zc').val();
		    var hos=$('#hos').val();
			var ks=$("#ks").find("option:selected").text();
			$.ajax({
				   type: 'post',
				   url: "<%=basePath %>updateZhuanjia2zhuanjiaAction",
				   data: "info="+id+"\t"+name+"\t"+sex+"\t"+lczc+"\t"+birthday+"\t"+worktime+"\t"+xueli+"\t"+telphone+"\t"+introduce+"\t"+zc+"\t"+hos+"\t"+ks,
				   success: function(data)
				   {
				     
			    		if(data=='ok')
			    		{
			    			$.messager.alert('后台提示', '数据更新成功', 'info');
			    			//初始化
			    			$('#dd').dialog('close');
			    			$('#test').datagrid('reload');
			    		}
						
						if(data=='error')
						{
			    			$.messager.alert('后台提示', '数据更新失败', 'info');
			    			$('#dd').dialog('close');
			    		}
					}
					
				  });	
	}	
    /*单个删除操作*/
    function delRow(index)
    {
           var selected = $('#test').datagrid('getSelected');//获取你选择的所有行

           if(selected!=null)
           {
	             $.messager.confirm('确认','你确定要删除吗?',function(r)
				  {
					  if(r)
					  {
					      $.ajax({
								   type: 'post',
								   url: "delZhuanjiazhuanjiaAction",
								   data: "delZhuanIds="+selected.id,
								   success: function(data)
								   {
								     if(data=='ok')
								     {
										$.messager.alert('提醒','删除成功','info');
										//删除所选的行
										var index2 = $('#test').datagrid('getRowIndex',selected);//获取某行的行号
										$('#test').datagrid('deleteRow',index2);	//通过行号移除该行
									}
									if(data=='error')
									{
										$.messager.alert('提醒','删除失败','info');
									}
								  }
							});
					  }
				  })
			}
			else
			{
			  $.messager.alert('后台提示', '请在要删除的行前打钩', 'info');
			  return false;
			}			
		 
    }
		/*编辑医院信息*/
		function editRow(index)
		{
        var rows = $('#test').datagrid('getSelections');
		 if(rows.length==1)
		 {
			editing();			
		 }
		 else
		 {
			$.messager.alert('提醒','请选择一行','info');
			return false;
		 }
        }


    /*保存医院信息*/
     function saveRow(index)
     {
      
     $('#test').datagrid('endEdit',index);
        /*
         * 将数据保存到数据库
         * */
        var select = $('#test').datagrid('getSelected');
        var arr =new Array(); //将信息保存在数组中
        arr[0]= select.id;
        arr[1]= select.name;
        arr[2]= select.leve;
        arr[3]= select.address;
        arr[4]= select.telphone;
        arr[5]=select.weburl;
        if(select){
            $.ajax({
            type:'POST',
            url:'<%=basePath%>addHospiatlhospitalAction',
            data:'id='+select.id,
            success:function(data){
                if(data=='0'){
                    $.messager.alert('error','学号'+select.id+'已存在,请重新编辑','error',function(){
                        $('#tt').datagrid('beginEdit', index);
                    });
                }else{
                    save(arr,index);
                }
            }
        });
        }
        
       
    }
  
	
        /*保存医院*/
        function save(arr,index)
        {
             $.ajax({
            type:'POST',
            url:'saveInfo.action',
            data:'arr='+arr,
            success:function(data){
                if(data){
                    $.messager.alert('warning',data,'warning',function(){
                        $('#tt').datagrid('beginEdit', index);
                    })
                }else{
                    $('#tt').datagrid('reload');
                }
            }
        });
    }
   
    /*查询*/
     function query()
     {
  
        var queryParams = $('#test').datagrid('options').queryParams;
        queryParams.queryType = $('#queryType').val();
        queryParams.queryValue = $('#queryValue').val();
        	$('#test').datagrid(
        	{
        	     
            	url:'<%=basePath%>queryZhuanjiazhuanjiaAction'
        	});
        	$('#query').window('close');        
        
    }
    
      
  
	</script>
</head>
<body>
	<table id="test"></table>
	
	 <div id="dd" icon="icon-edit" title="编辑医生信息" style="display:none;padding-top:30px;width:750px;height:450px;">
  <div style="width:700px;height:auto;background:#fcfcfc;" >
  <form action="<%=basePath %>updateZhuanjiazhuanjiaAction" id="UpdateZhuanjiaForm"  method="post">
		<table class="grid" id="uiform" >
			<tr>
				<td>
					*医生名称:
				</td>
				<td>
				    <input type="hidden" name="zhuanjia.id" id="id"/>
					<input id="name"  type="text" name="zhuanjia.name"   class="txt03" value="" />
				</td>
				<td>
					*医生性别：
				</td>
				<td>
					<input id="sex"  type="text" name="zhuanjia.sex"   class="txt03" value="" />
				</td>
			</tr>
			<tr>
				<td>
				*所属医院：
				</td>
				<td>
					<select style="width:150px;" name="zhuanjia.keshi.hospital" id="hos" onChange="changeHospital()">
					<c:forEach  items="${hs}" var="zs">
					<option value="${zs.name}">${zs.name }</option>
					</c:forEach>
					</select>
				</td>
				<td>
					*所属科室：
				</td>
				<td>
				<select style="width:150px;" name="zhuanjia.keshi" id="ks">
					<option>选择科室</option>
					</select>
				</td>
			</tr>
				<tr>
				<td>
					*出生日期:：
				</td>
				<td>
					<input id="birthday"   maxlength="20" name="zhuanjia.birthday" class="txt03"  value=""  />
				</td>
					<td>
					*从业年限：
				</td>
				<td>
					<input id="worktime"   name="zhuanjia.worktime"  class="txt03"  value=""  />
				</td>
			</tr>
			<tr>
			
			    <td>
					*学历学位：
				</td>
				<td>
					<input id="xueli"   name="zhuanjia.xueli"  class="txt03"  value=""  />
				</td>
				  <td>
					*技术职称：
				</td>
				<td>
					<input id="lczc"   name="zhuanjia.lczc"  class="txt03"  value=""  />
				</td>
			</tr>
					<tr>
			    <td>
					*联系电话：
				</td>
				<td>
					<input id="telphone"   name="zhuanjia.telphone"  class="txt03"  value=""  />
				</td>
				  <td>
					*教学简介：
				</td>
				<td>
					<input id="introduce"   name="zhuanjia.introduce"  class="txt03"  value=""  />
				</td>
			</tr>
					<tr>
			    <td>
					*医生擅长：
				</td>
				<td>
					<input id="zc"   name="zhuanjia.zc"  class="txt03"  value=""  />
				</td>
				</tr>
			<tr>
			    <td>
				<input id="etj"   name="etj" type="button"  value="提交" onclick="updateZhuanjia();"/>
				</td>
				<td>
					<input id="eqx"   name="eqx" type="button"  value="取消"/>
				</td>
			
			</tr>	
		
		</table>
		</form>
</div></div>
	
	<div id="query" class="easyui-window" title="查询" style="padding: 10px;width: 400px;height:120;"
    iconCls="icon-search" closed="true" maximizable="false" minimizable="false" collapsible="false">
        <div>
            <table>
                <tr>
                    <td>
                        <select name="select" id="queryType">
                            <option value="id">编号</option>
                            <option value="name">专家名称</option>
                         
                        </select>
                    </td>
                    <td><input type="text" style="background:#fcfcfc;" name="queryValue" id="queryValue"  value="" /></td>
                    <td><a class="easyui-linkbutton" iconCls="icon-search" href="javascript:void(0);" onclick="query()">查询</a></td>
                </tr>
            </table>
        </div>
    </div>
    
    <div id="add" icon="icon-edit" style="display:none;padding-top:30px;width:600px;height:350px;" title="增加医生">
  <div style="width:550px;height:auto;background:#fcfcfc;" >
  <form action="<%=basePath %>addZhuanjiazhuanjiaAction" id="addZhuanjiaFrom"  method="post">
		<table class="grid" id="uiform">
			<tr>
				<td>
					*医生名称：
				</td>
				<td>
				
					<input id="name1"  type="text" name="zhuanjia.name"   class="txt03" value="" />
				</td>
				<td>
					*医生性别：
				</td>
				<td>
					<input id="sex1"  type="text" name="zhuanjia.sex"   class="txt03" value="" />
				</td>
			</tr>
			<tr>
				<td>
				*所属医院：
				</td>
				<td>
					<select style="width:150px;" name="hospital" id="hospital" onChange="changeHospital()">
					<option>请选择医院</option>
					<c:forEach  items="${hs}" var="zs">
					<option value="${zs.name}">${zs.name }</option>
					</c:forEach>
					</select>
				</td>
				<td>
					*所属科室：
				</td>
				<td>
				<select style="width:150px;" name="keshi" id="keshi">
					<option>选择科室</option>
					</select>
				</td>
			</tr>
				<tr>
				<td>
					*出生日期:：
				</td>
				<td>
					<input id="birthday1"   maxlength="20" name="zhuanjia.birthday" class="txt03"  value=""  />
				</td>
					<td>
					*从业年限：
				</td>
				<td>
					<input id="worktime1"   name="zhuanjia.worktime"  class="txt03"  value=""  />
				</td>
			</tr>
			<tr>
			
			    <td>
					*学历学位：
				</td>
				<td>
					<input id="xueli1"   name="zhuanjia.xueli"  class="txt03"  value=""  />
				</td>
				  <td>
					*技术职称：
				</td>
				<td>
					<input id="lczc1"   name="zhuanjia.lczc"  class="txt03"  value=""  />
				</td>
			</tr>
					<tr>
			
			    <td>
					*联系电话：
				</td>
				<td>
					<input id="telphone1"   name="zhuanjia.telphone"  class="txt03"  value=""  />
				</td>
				  <td>
					*教学简介：
				</td>
				<td>
					<input id="introduce1"   name="zhuanjia.introduce"  class="txt03"  value=""  />
				</td>
			</tr>
					<tr>
			    <td>
					*医生擅长：
				</td>
				<td>
					<input id="zc1"   name="zhuanjia.zc"  class="txt03"  value=""  />
				</td>
			
			</tr>
				<tr>
			    <td>
				<input id="tj"   name="tj" type="button"  value="提交" onclick="add();"/>
				</td>
				<td>
					<input id="qx"   name="qx" type="button"  value="取消"/>
				</td>
			
			</tr>	
		</table>
		</form>
</div></div>
</body>
</html>